﻿#region Using Directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;

using NiSis.Entities;
using NiSis.Data;
using NiSis.Data.Bases;
#endregion

namespace NiSis.Web.Data
{
	/// <summary>
	/// Represents the DataRepository.SshUtenzeGasProvider object that provides
	/// data to data-bound controls in multi-tier Web application architectures.
	/// </summary>
	[Designer(typeof(SshUtenzeGasDataSourceDesigner))]
	public class SshUtenzeGasDataSource : ProviderDataSource<SshUtenzeGas, SshUtenzeGasKey>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the SshUtenzeGasDataSource class.
		/// </summary>
		public SshUtenzeGasDataSource() : base(DataRepository.SshUtenzeGasProvider)
		{
		}

		#endregion Constructors
		
		#region Properties
		
		/// <summary>
		/// Gets a reference to the SshUtenzeGasDataSourceView used by the SshUtenzeGasDataSource.
		/// </summary>
		protected SshUtenzeGasDataSourceView SshUtenzeGasView
		{
			get { return ( View as SshUtenzeGasDataSourceView ); }
		}
		
		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the SshUtenzeGasDataSource control invokes to retrieve data.
		/// </summary>
		public SshUtenzeGasSelectMethod SelectMethod
		{
			get
			{
				SshUtenzeGasSelectMethod selectMethod = SshUtenzeGasSelectMethod.GetAll;
				Object method = ViewState["SelectMethod"];
				if ( method != null )
				{
					selectMethod = (SshUtenzeGasSelectMethod) method;
				}
				return selectMethod;
			}
			set { ViewState["SelectMethod"] = value; }
		}

		#endregion Properties
		
		#region Methods

		/// <summary>
		/// Creates a new instance of the SshUtenzeGasDataSourceView class that is to be
		/// used by the SshUtenzeGasDataSource.
		/// </summary>
		/// <returns>An instance of the SshUtenzeGasDataSourceView class.</returns>
		protected override BaseDataSourceView<SshUtenzeGas, SshUtenzeGasKey> GetNewDataSourceView()
		{
			return new SshUtenzeGasDataSourceView(this, DefaultViewName);
		}
		
		/// <summary>
        /// Creates a cache hashing key based on the startIndex, pageSize and the SelectMethod being used.
        /// </summary>
        /// <param name="startIndex">The current start row index.</param>
        /// <param name="pageSize">The current page size.</param>
        /// <returns>A string that can be used as a key for caching purposes.</returns>
		protected override string CacheHashKey(int startIndex, int pageSize)
        {
			return String.Format("{0}:{1}:{2}", SelectMethod, startIndex, pageSize);
        }
		
		#endregion Methods
	}
	
	/// <summary>
	/// Supports the SshUtenzeGasDataSource control and provides an interface for
	/// data-bound controls to perform data operations with business and data objects.
	/// </summary>
	public class SshUtenzeGasDataSourceView : ProviderDataSourceView<SshUtenzeGas, SshUtenzeGasKey>
	{
		#region Declarations

		#endregion Declarations
		
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the SshUtenzeGasDataSourceView class.
		/// </summary>
		/// <param name="owner">A reference to the SshUtenzeGasDataSource which created this instance.</param>
		/// <param name="viewName">The name of the view.</param>
		public SshUtenzeGasDataSourceView(SshUtenzeGasDataSource owner, String viewName)
			: base(owner, viewName)
		{
		}
		
		#endregion Constructors
		
		#region Properties

		/// <summary>
		/// Gets a strongly-typed reference to the Owner property.
		/// </summary>
		internal SshUtenzeGasDataSource SshUtenzeGasOwner
		{
			get { return Owner as SshUtenzeGasDataSource; }
		}

		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the DataSource control invokes to retrieve data.
		/// </summary>
		internal SshUtenzeGasSelectMethod SelectMethod
		{
			get { return SshUtenzeGasOwner.SelectMethod; }
			set { SshUtenzeGasOwner.SelectMethod = value; }
		}

		/// <summary>
		/// Gets a strongly typed reference to the Provider property.
		/// </summary>
		internal SshUtenzeGasProviderBase SshUtenzeGasProvider
		{
			get { return Provider as SshUtenzeGasProviderBase; }
		}

		#endregion Properties
		
		#region Methods
		
		/// <summary>
		/// Gets a collection of Entity objects based on the value of the SelectMethod property.
		/// </summary>
		/// <param name="count">The total number of rows in the DataSource.</param>
		/// <returns>A collection of Entity objects.</returns>
		protected override IList<SshUtenzeGas> GetSelectData(out int count)
		{
			Hashtable values = CollectionsUtil.CreateCaseInsensitiveHashtable(GetParameterValues());
			Hashtable customOutput = CollectionsUtil.CreateCaseInsensitiveHashtable();
			IList<SshUtenzeGas> results = null;
			SshUtenzeGas item;
			count = 0;
			
			System.String _cdMeseInizioFornitura;
			System.String _cdPuntoFornitura;
			System.String _cdComune_nullable;
			System.String _cdPrelievoAeeg_nullable;

			switch ( SelectMethod )
			{
				case SshUtenzeGasSelectMethod.Get:
					SshUtenzeGasKey entityKey  = new SshUtenzeGasKey();
					entityKey.Load(values);
					item = SshUtenzeGasProvider.Get(GetTransactionManager(), entityKey);
					results = new TList<SshUtenzeGas>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				case SshUtenzeGasSelectMethod.GetAll:
                    results = SshUtenzeGasProvider.GetAll(GetTransactionManager(), StartIndex, PageSize, out count);
                    break;
				case SshUtenzeGasSelectMethod.GetPaged:
					results = SshUtenzeGasProvider.GetPaged(GetTransactionManager(), WhereClause, OrderBy, PageIndex, PageSize, out count);
					break;
				case SshUtenzeGasSelectMethod.Find:
					if ( FilterParameters != null )
						results = SshUtenzeGasProvider.Find(GetTransactionManager(), FilterParameters, OrderBy, StartIndex, PageSize, out count);
					else
						results = SshUtenzeGasProvider.Find(GetTransactionManager(), WhereClause, StartIndex, PageSize, out count);
                    break;
				// PK
				case SshUtenzeGasSelectMethod.GetByCdMeseInizioFornituraCdPuntoFornitura:
					_cdMeseInizioFornitura = ( values["CdMeseInizioFornitura"] != null ) ? (System.String) EntityUtil.ChangeType(values["CdMeseInizioFornitura"], typeof(System.String)) : string.Empty;
					_cdPuntoFornitura = ( values["CdPuntoFornitura"] != null ) ? (System.String) EntityUtil.ChangeType(values["CdPuntoFornitura"], typeof(System.String)) : string.Empty;
					item = SshUtenzeGasProvider.GetByCdMeseInizioFornituraCdPuntoFornitura(GetTransactionManager(), _cdMeseInizioFornitura, _cdPuntoFornitura);
					results = new TList<SshUtenzeGas>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				// IX
				case SshUtenzeGasSelectMethod.GetByCdComune:
					_cdComune_nullable = (System.String) EntityUtil.ChangeType(values["CdComune"], typeof(System.String));
					results = SshUtenzeGasProvider.GetByCdComune(GetTransactionManager(), _cdComune_nullable, this.StartIndex, this.PageSize, out count);
					break;
				case SshUtenzeGasSelectMethod.GetByCdPrelievoAeeg:
					_cdPrelievoAeeg_nullable = (System.String) EntityUtil.ChangeType(values["CdPrelievoAeeg"], typeof(System.String));
					results = SshUtenzeGasProvider.GetByCdPrelievoAeeg(GetTransactionManager(), _cdPrelievoAeeg_nullable, this.StartIndex, this.PageSize, out count);
					break;
				// FK
				// M:M
				// Custom
				default:
					break;
			}

			if ( results != null && count < 1 )
			{
				count = results.Count;

				if ( !String.IsNullOrEmpty(CustomMethodRecordCountParamName) )
				{
					object objCustomCount = EntityUtil.ChangeType(customOutput[CustomMethodRecordCountParamName], typeof(Int32));
					
					if ( objCustomCount != null )
					{
						count = (int) objCustomCount;
					}
				}
			}
			
			return results;
		}
		
		/// <summary>
		/// Gets the values of any supplied parameters for internal caching.
		/// </summary>
		/// <param name="values">An IDictionary object of name/value pairs.</param>
		protected override void GetSelectParameters(IDictionary values)
		{
			if ( SelectMethod == SshUtenzeGasSelectMethod.Get || SelectMethod == SshUtenzeGasSelectMethod.GetByCdMeseInizioFornituraCdPuntoFornitura )
			{
				EntityId = GetEntityKey(values);
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation for the current entity if it has
		/// not already been performed.
		/// </summary>
		internal override void DeepLoad()
		{
			if ( !IsDeepLoaded )
			{
				SshUtenzeGas entity = GetCurrentEntity();
				
				if ( entity != null )
				{
					// execute deep load method
					SshUtenzeGasProvider.DeepLoad(GetTransactionManager(), GetCurrentEntity(), EnableRecursiveDeepLoad);
					// set loaded flag
					IsDeepLoaded = true;
				}
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation on the specified entity collection.
		/// </summary>
		/// <param name="entityList"></param>
		/// <param name="properties"></param>
		internal override void DeepLoad(TList<SshUtenzeGas> entityList, ProviderDataSourceDeepLoadList properties)
		{
			// execute deep load method
			SshUtenzeGasProvider.DeepLoad(GetTransactionManager(), entityList, properties.Recursive, properties.Method, properties.GetTypes());
		}

		#endregion Select Methods
	}
	
	#region SshUtenzeGasDataSourceDesigner

	/// <summary>
	/// Provides design-time support in a design host for the SshUtenzeGasDataSource class.
	/// </summary>
	public class SshUtenzeGasDataSourceDesigner : ProviderDataSourceDesigner<SshUtenzeGas, SshUtenzeGasKey>
	{
		/// <summary>
		/// Initializes a new instance of the SshUtenzeGasDataSourceDesigner class.
		/// </summary>
		public SshUtenzeGasDataSourceDesigner()
		{
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public SshUtenzeGasSelectMethod SelectMethod
		{
			get { return ((SshUtenzeGasDataSource) DataSource).SelectMethod; }
			set { SetPropertyValue("SelectMethod", value); }
		}

		/// <summary>Gets the designer action list collection for this designer.</summary>
		/// <returns>The <see cref="T:System.ComponentModel.Design.DesignerActionListCollection"/>
		/// associated with this designer.</returns>
		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection actions = new DesignerActionListCollection();
				actions.Add(new SshUtenzeGasDataSourceActionList(this));
				actions.AddRange(base.ActionLists);
				return actions;
			}
		}
	}

	#region SshUtenzeGasDataSourceActionList

	/// <summary>
	/// Supports the SshUtenzeGasDataSourceDesigner class.
	/// </summary>
	internal class SshUtenzeGasDataSourceActionList : DesignerActionList
	{
		private SshUtenzeGasDataSourceDesigner _designer;

		/// <summary>
		/// Initializes a new instance of the SshUtenzeGasDataSourceActionList class.
		/// </summary>
		/// <param name="designer"></param>
		public SshUtenzeGasDataSourceActionList(SshUtenzeGasDataSourceDesigner designer) : base(designer.Component)
		{
			_designer = designer;
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public SshUtenzeGasSelectMethod SelectMethod
		{
			get { return _designer.SelectMethod; }
			set { _designer.SelectMethod = value; }
		}

		/// <summary>
		/// Returns the collection of <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// objects contained in the list.
		/// </summary>
		/// <returns>A <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// array that contains the items in this list.</returns>
		public override DesignerActionItemCollection GetSortedActionItems()
		{
			DesignerActionItemCollection items = new DesignerActionItemCollection();
			items.Add(new DesignerActionPropertyItem("SelectMethod", "Select Method", "Methods"));
			return items;
		}
	}

	#endregion SshUtenzeGasDataSourceActionList
	
	#endregion SshUtenzeGasDataSourceDesigner
	
	#region SshUtenzeGasSelectMethod
	
	/// <summary>
	/// Enumeration of method names available for the SshUtenzeGasDataSource.SelectMethod property.
	/// </summary>
	public enum SshUtenzeGasSelectMethod
	{
		/// <summary>
		/// Represents the Get method.
		/// </summary>
		Get,
		/// <summary>
		/// Represents the GetAll method.
		/// </summary>
		GetAll,
		/// <summary>
		/// Represents the GetPaged method.
		/// </summary>
		GetPaged,
		/// <summary>
		/// Represents the Find method.
		/// </summary>
		Find,
		/// <summary>
		/// Represents the GetByCdMeseInizioFornituraCdPuntoFornitura method.
		/// </summary>
		GetByCdMeseInizioFornituraCdPuntoFornitura,
		/// <summary>
		/// Represents the GetByCdComune method.
		/// </summary>
		GetByCdComune,
		/// <summary>
		/// Represents the GetByCdPrelievoAeeg method.
		/// </summary>
		GetByCdPrelievoAeeg
	}
	
	#endregion SshUtenzeGasSelectMethod

	#region SshUtenzeGasFilter
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilter&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="SshUtenzeGas"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class SshUtenzeGasFilter : SqlFilter<SshUtenzeGasColumn>
	{
	}
	
	#endregion SshUtenzeGasFilter

	#region SshUtenzeGasExpressionBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlExpressionBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="SshUtenzeGas"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class SshUtenzeGasExpressionBuilder : SqlExpressionBuilder<SshUtenzeGasColumn>
	{
	}
	
	#endregion SshUtenzeGasExpressionBuilder	

	#region SshUtenzeGasProperty
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ChildEntityProperty&lt;SshUtenzeGasChildEntityTypes&gt;"/> class
	/// that is used exclusively with a <see cref="SshUtenzeGas"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class SshUtenzeGasProperty : ChildEntityProperty<SshUtenzeGasChildEntityTypes>
	{
	}
	
	#endregion SshUtenzeGasProperty
}

